package com.yidu.t299.controller.xp;
import com.yidu.t299.domain.xp.*;
import com.yidu.t299.domain.xp.Class;
import com.yidu.t299.service.xp.StucatabolizeManageBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @descripion: 学员退班管理控制器
 * @auth: XuPan
 * @date: 2023/3/13 8:35
 * @version: 1.0
 */
@RestController
public class StucatabolizeManageController {
    @Autowired
    private StucatabolizeManageBiz stucatabolizeManageBiz;//注入数据

    /**
     * 初始化操作，跳转并保存下拉框数据(实现动态下拉框功能)
     */
    @RequestMapping("stucatabolizeManage/init")
    public ModelAndView saveSelect() {
        List<School> studentManageListOne = stucatabolizeManageBiz.selectAllGetSchoolName();//得到所有校区名称
        List<Class> studentManageListTwo = stucatabolizeManageBiz.selectAllGetClassName();//得到所有班级名称
        List<Record> studentManageListThird = stucatabolizeManageBiz.selectAllGetRecordName();//得到所有教师名称
        ModelAndView mv = new ModelAndView();
        mv.setViewName("stucatabolizeManage");//跳转的页面
        mv.addObject("studentManageListOne", studentManageListOne); //保存校区信息
        mv.addObject("studentManageListTwo", studentManageListTwo); //保存班级信息
        mv.addObject("studentManageListThird", studentManageListThird); //保存教师信息
        return mv;
    }

    /**
     * 分页查询显示数据在表格中
     *
     * @param limit 页码
     * @param page  每页的行数
     * @param stucatabolizeManage   接收多条件搜索的表单数据对象
     * @return 映射集合对象
     */
    @GetMapping("stucatabolizeManage/restful_StucatabolizeManage")
    public Map<String, Object> findAll(int limit, int page, StucatabolizeManage stucatabolizeManage) {
        // 根据stucatabolizeManage对象得到条件字符串
        String condition = stucatabolizeManageBiz.getCondition(stucatabolizeManage);
        // 按条件统计总记录行数
        int count = stucatabolizeManageBiz.selectCount(condition);
        // 按条件分页查询
        List<StucatabolizeManage> stucatabolizeManageList = stucatabolizeManageBiz.selectByPage(limit, page-1, condition);
        // 创建一个map，用来存放返回给layui组件需要的键值对
        Map<String, Object> mapData = new HashMap<String, Object>();
        //在映射集合中添加key为code，值为0的状态码
        mapData.put("code", 0);
        //在映射集合中添加key为msg，值为空字符串的响应信息字符串
        mapData.put("msg", "");
        //在映射集合中添加为count，值为总行数的映射
        mapData.put("count", count);
        //在映射集合中添加key为data，值为退班对象集合的映射
        mapData.put("data", stucatabolizeManageList);
        //把映射集合响应到客户端
        return mapData;
    }

    /**
     * 新增
     * @param stucatabolizeManage
     * @return
     */
    @PostMapping("stucatabolizeManage/restful_StucatabolizeManage")
    public String addEmp(@RequestBody StucatabolizeManage stucatabolizeManage) {
        //得到校区名称 班级名称 教师名称 判断是否为空不为空则进行操作
        if (stucatabolizeManage!=null){
            if (stucatabolizeManage.getSchoolName()!=null && stucatabolizeManage.getClassName()!=null && stucatabolizeManage.getRecordName()!=null){
                //查询出教师姓名 班级名称 校区名称 存入stucatabolizeManage中
                stucatabolizeManage.setSchoolId(stucatabolizeManageBiz.selectBySchoolName(stucatabolizeManage.getSchoolName()));
                stucatabolizeManage.setClassId(stucatabolizeManageBiz.selectByClassName(stucatabolizeManage.getClassName()));
                stucatabolizeManage.setRecordId(stucatabolizeManageBiz.selectByRecordName(stucatabolizeManage.getRecordName()));
            }
        }
        //调用业务层实现新增退班，并返回正确与否字符串
        return stucatabolizeManageBiz.insert(stucatabolizeManage) > 0 ? "添加成功" : "添加失败";
    }

    /**
     * 修改
     * @param stucatabolizeManage
     * @return
     */
    @PutMapping("stucatabolizeManage/restful_StucatabolizeManage")
    public String updateEmp(@RequestBody StucatabolizeManage stucatabolizeManage) {
        //得到校区名称 班级名称 教师名称 判断是否为空不为空则进行操作
        if (stucatabolizeManage!=null){
            if (stucatabolizeManage.getSchoolName()!=null && stucatabolizeManage.getClassName()!=null && stucatabolizeManage.getRecordName()!=null){
                //查询出教师姓名 班级名称 校区名称 存入stucatabolizeManage中
                stucatabolizeManage.setSchoolId(stucatabolizeManageBiz.selectBySchoolName(stucatabolizeManage.getSchoolName()));
                stucatabolizeManage.setClassId(stucatabolizeManageBiz.selectByClassName(stucatabolizeManage.getClassName()));
                stucatabolizeManage.setRecordId(stucatabolizeManageBiz.selectByRecordName(stucatabolizeManage.getRecordName()));
            }
        }
        //调用业务层实现新增退班，并返回正确与否字符串
        return stucatabolizeManageBiz.updateByPrimaryKey(stucatabolizeManage) > 0 ? "修改成功" : "修改失败";
    }

    /**
     *删除
     * @param stucataId
     * @return
     */
    @DeleteMapping("stucatabolizeManage/restful_StucatabolizeManage/{stucataId}")
    public String deleteEmpById(@PathVariable("stucataId") int stucataId) {
        //调用业务层实现删除退班，并返回正确与否字符串
        return stucatabolizeManageBiz.deleteByPrimaryKey(stucataId) > 0 ? "删除成功" : "删除失败";
    }

    /**
     *批量删除
     * @param schoolIdStr
     * @return
     */
    @PutMapping("stucatabolizeManage/batchDel")
    public String deleteEmpByManyId(@RequestParam String schoolIdStr) {
        //按,号分割成字符串数组
        String[] strArrays = schoolIdStr.split(",");
        //循环按退班编号删除
        for (String str : strArrays) {
            //只要有一个删除错误就直接返回错误信息字符串
            if (stucatabolizeManageBiz.deleteByPrimaryKey(Integer.parseInt(str)) < 1) {
                return "批量删除失败";
            }
        }
        return "批量删除成功";
    }
}
